<template>
  <image :src="newSrc" @tap="onClick" :mode="props.mode" />
</template>
<script setup>
import { computed } from 'vue';
import { IMG_PREFIX } from '@/utils/config';
import { isHttp } from '@/utils';
import { previewImage } from '@tarojs/taro';

const props = defineProps({
  src: String,
  mode: {
    type: String,
    default: 'aspectFill'
  },
  preview: Boolean,
  defaultSrc: {
    type: String,
    default: '/assets/imgs/default/img-err.png'
  }
});

const emit = defineEmits(['tap']);
const newSrc = computed(() => {
  if (!props.src) {
    return props.defaultSrc;
  }
  if (!/^[a-f0-9]+$/.test(props.src)) return props.src;
  return IMG_PREFIX.value + props.src;
});
const onClick = e => {
  if (props.preview) {
    previewImage({
      current: 0,
      urls: [newSrc.value]
    });
  }
  emit('tap', e);
};
</script>
<style lang="scss"></style>
